<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>案例，灯泡</title>
</head>
<body>
    <img id="myImg" src="../img/2.png" alt="灯泡">

    <script>
        /*
            开灯
         */
        function on() {
            //document.getElementById() 获取标签的id
            document.getElementById("myImg").src='../img/m1.png';
        }
        /*
            关灯
         */
        function off() {
            document.getElementById("myImg").src='../img/m2.png';
        }
        //定义一个变量控制开关
        var x=0;
        //使用循环定时器
        setInterval(function (){
            if (x%2==0){
                on()
            }else {
                off()
            }
            x++;
        },2000)



    </script>
</body>
</html>